package com.theme.passport.service.impl;

import com.theme.common.third.weather.WeatherProperties;
import com.theme.common.third.weather.WeatherRequest;
import com.theme.passport.service.IWeatherApi;
import lombok.extern.java.Log;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Resource;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.time.Duration;
import java.util.*;
import java.util.logging.Logger;

import static com.theme.passport.service.theme.impl.AppleServiceImpl.logger;
import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;

public class WeatherServiceImpl {

    @Resource
    private WeatherProperties weatherProperties;

//    @Resource
//    private IWeatherApi weatherApi;


//    public Map<String, Object> weather(WeatherRequest request) {
//        try {
//            String cityWeatherCacheKey = StringUtils.joinWith(":", "weather", request.getCity());
//            Map<String, Object> cacheObject = getWeatherFromCache(cityWeatherCacheKey);
//            if (Objects.nonNull(cacheObject) && !cacheObject.isEmpty()) {
//                return cacheObject;
//            }
//            String token = getToken();
//            Map<String, String> requestParams = new HashMap<>();
//            requestParams.put("countryCode", request.getCity());
//            requestParams.put("dataSets", "currentWeather,forecastHourly");
//            cacheObject = weatherApi.getWeatherByLocation("Bearer " + token, request.getLang(), request.getLat(), request.getLon(), requestParams);
////            RedisUtils.setCacheObject(cityWeatherCacheKey, cacheObject, Duration.ofSeconds(weatherProperties.getTokenTTL() - 60));
////            Redis
//            return cacheObject;
//        } catch (Exception e) {
//            logger.error("get weather info error", e);
//            return null;
//        }
//    }

//    private Map<String, Object> getWeatherFromCache(String cityWeatherCacheKey) {
//        Object cacheObject = RedisUtils.getCacheObject(cityWeatherCacheKey);
//        return Objects.nonNull(cacheObject) ? (Map<String, Object>) cacheObject : null;
//    }
//
//    private String getToken() throws Exception {
//        final String cacheKey = StringUtils.joinWith(":", "weather", "token");
//        Object cacheObject = RedisUtils.getCacheObject(cacheKey);
//        if (Objects.isNull(cacheObject)) {
//
//            // Define JWT claims
//            long currentTimeMillis = System.currentTimeMillis();
//            Date now = new Date(currentTimeMillis);
//            Date expiration = new Date(currentTimeMillis + weatherProperties.getTokenTTL() * 1000L); // 1-hour expiration
//
//
//            byte[] keyBytes = Base64.getDecoder().decode(weatherProperties.getKey());
//            PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
//            KeyFactory keyFactory = KeyFactory.getInstance("EC");  // Apple uses EC (Elliptic Curve) algorithm
//            PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
//            JWTSigner signer = JWTSignerUtil.createSigner("ES256", privateKey);
//
//
//            Map<String, Object> header = new HashMap<>();
//            header.put("alg", "ES256"); // Algorithm used for signing
//            header.put("kid", weatherProperties.getKeyId());  // Key ID
//
//            Map<String, Object> payload = new HashMap<>();
//            payload.put("iss", weatherProperties.getTeamId());    // Team ID
//            payload.put("sub", weatherProperties.getBundleId());  // Issuer ID
//            payload.put("iat", now.getTime() / 1000); // Issued at time in seconds
//            payload.put("exp", expiration.getTime() / 1000); // Expiration time in seconds
//
//            String token = JWTUtil.createToken(header, payload, signer);
//
//            // 提前60s过期
//            RedisUtils.setCacheObject(cacheKey, token, Duration.ofSeconds(weatherProperties.getTokenTTL() - 60));
//            return token;
//        }
//
//        return (String) cacheObject;
//    }

}



